//
//  HotListViewController.m
//  225视听百科
//
//  Created by 张元阳(10) on 14-12-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HotListViewController.h"
#import "CustomImageAndLabel.h"
#import "ConnectModel.h"
#import "UIImageView+WebCache.h"
#import "List2ViewController.h"

@interface HotListViewController ()

@end

@implementation HotListViewController
- (void)dealloc
{
    [_name release];
    [_listArray release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.listArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createList];
}
- (void)createList
{
    self.navigationItem.title = self.name;
    NSMutableDictionary *dicAll = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"全部", @"name", @"http://img6.cache.netease.com/3g/2014/12/29/201412291207187140b.jpg", @"cover", nil];
    [_listArray addObject:dicAll];
    
    NSString *str = [NSString stringWithFormat:@"http://app.9nali.com/777/category/%ld/tags?page_id=1&device=iPhone&version=1.1.5", self.catetory_id];
    [ConnectModel connectModelWithUrl:str parmaters:nil finished:^(NSData *getData) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:getData options:NSJSONReadingMutableLeaves error:nil];
        NSMutableArray *array = [dic objectForKey:@"tags"];
        [_listArray addObjectsFromArray:array];
        
        NSInteger line = [_listArray count] / 2;
        if ([_listArray count] % 2 > 0) {
            line++;
        }
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        scroll.contentSize = CGSizeMake(self.view.frame.size.width, line * 60 + 10);
        [self.view addSubview:scroll];
        for (int i = 0; i < [_listArray count]; i++) {
            NSDictionary *dic = [_listArray objectAtIndex:i];
            CustomImageAndLabel *button = [[CustomImageAndLabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width / 2) * (i % 2),10 + 60 * (i / 2), self.view.frame.size.width / 2 - 10, 50)];
            button.tag = [[dic objectForKey:@"position"] integerValue];
            NSString *str = [dic objectForKey:@"cover"];
            button.label.text = [dic objectForKey:@"name"];
            [button.imageV setImageWithURL:[NSURL URLWithString:str]];
            [scroll addSubview:button];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            [button release];
        }
        
        [scroll release];
    }];
    
    
    
}
- (void)buttonAction:(CustomImageAndLabel *)sender
{
    List2ViewController *list = [[List2ViewController alloc] init];
    [self.navigationController pushViewController:list animated:YES];
    list.name = sender.label.text;
    list.category_id = self.catetory_id;
    [list release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
